<?php
require_once 'models/order.inc';
require_once 'models/orm.inc';

class TMSDeliveryMethodItem extends TCandyItem {
	protected function checkProperties(){
		parent::checkProperties();
		foreach ($this->Base->SetupData as $nm=>$data){
			$this->properties[$nm] = new TCandyItemProperty($this, new TPropertyMeta($nm, $data->caption, $data->type));
		}
	}	
}


class TMSDeliveryModel extends  TCandyModel {
	protected function afterConstruct(){
		$this->_conf_classname_ = 'TMSDeliveryMethod';
		$this->_array_id_properties_ = array('Code');
	}
	
	protected function postProcessProperties(){
		$this->propertyMetas['Code']->ReadOnly = true;
		$this->propertyMetas['Component']->ReadOnly = true;
	}	
	
	/**
	 * @param object $object
	 * @return TCandyItem
	 */
	public function WrapObject($object, array &$caller_stack = array()){
		if ($object instanceof TMSDeliveryMethod)
			return new TMSDeliveryMethodItem($object,$this);
		return parent::WrapObject($object,$caller_stack);
	}

	protected function preEditItem($id,array $data, $origin){
		$setup = $origin->SetupData;
		foreach ($setup as $nm=>$dt)
			if (isset($data[$nm])){
				$setup[$nm]->value = $data[$nm];
				unset($data[$nm]);
			}
		$data['Setup'] = json_encode($setup);
		return $data;
	}
}